package github.kawaiior.yuanchuxiuzhen.common.item.weapon.refine;

import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.particles.IParticleData;
import net.minecraft.particles.ParticleTypes;
import net.minecraft.util.ActionResult;
import net.minecraft.util.DamageSource;
import net.minecraft.util.Hand;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld;

import javax.annotation.Nullable;
import java.util.List;

public class BurningMountainSword extends RefineWeaponBase{

    @Override
    public float getAttackDamage(){
        return 24F;
    }

    @Override
    public boolean hitEntity(ItemStack stack, LivingEntity target, LivingEntity attacker) {
        target.setFire(2);
        return super.hitEntity(stack, target, attacker);
    }

    @Override
    public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {
        if (worldIn.isRemote){
            return super.onItemRightClick(worldIn, playerIn, handIn);
        }
        AxisAlignedBB boundingBox = playerIn.getBoundingBox().grow(8.0);
        List<Entity> entities = worldIn.getEntitiesWithinAABB(Entity.class, boundingBox);
        for (Entity entity : entities) {
            if (entity instanceof LivingEntity && !entity.equals(playerIn) && !entity.isImmuneToFire()) {
                spawnParticlesAroundEntity((ServerWorld) worldIn, (LivingEntity)entity, ParticleTypes.FLAME);
                entity.setFire(5);
                entity.attackEntityFrom(DamageSource.causeIndirectMagicDamage(playerIn, playerIn), 50);
            }
        }
        return super.onItemRightClick(worldIn, playerIn, handIn);
    }

    public static <T extends IParticleData> void spawnParticlesAroundEntity(ServerWorld world, LivingEntity entity, T type) {
        int particleCount = 5;

        for (int i = 0; i < particleCount; ++i) {
            double offsetX = world.rand.nextGaussian() * 0.02;
            double offsetY = world.rand.nextGaussian() * 0.02;
            double offsetZ = world.rand.nextGaussian() * 0.02;

            world.spawnParticle(type,
                    entity.getPosX() + world.rand.nextDouble() * entity.getWidth() * 2.0 - entity.getWidth(),
                    entity.getPosY() + 0.5 + world.rand.nextDouble() * entity.getHeight(),
                    entity.getPosZ() + world.rand.nextDouble() * entity.getWidth() * 2.0 - entity.getWidth(),
                    10, offsetX, offsetY, offsetZ, 2D);
        }
    }

    @Override
    public void addInformation(ItemStack stack, @Nullable World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
        tooltip.add(new StringTextComponent("右键使周围生物燃烧并造成大量火焰伤害"));
    }
}
